package com.cxz.kotlin.baselibs.utils

import android.app.Activity
import android.content.Intent
import com.cxz.kotlin.baselibs.base.BaseConstant
import com.cxz.kotlin.baselibs.rx.bus.BusEvent
import com.cxz.kotlin.baselibs.rx.bus.RxBus
import com.jinduplan.planmanager.bean.Labour

/**
 * @author chenxz
 * @date 2019/1/10
 * @desc
 */
object UserInfoUtil {
    const val PROJECTID = "projectId"
    const val PROJECT_NAME = "PROJECT_NAME"

    var login: Boolean by Preference(BaseConstant.HAS_LOGIN, false)
    var userName: String by Preference(BaseConstant.USERNAME, "")
    var nick_name: String by Preference(BaseConstant.NIKENAME, "")
    var score: String by Preference(BaseConstant.SCORE, "")
    var position: String by Preference(BaseConstant.POSITION, "")
    var avatar: String by Preference(BaseConstant.AVATAR, "")
    var mProjectId: String by Preference(PROJECTID, "")
    var mProjectName: String by Preference(PROJECT_NAME, "")
    var fullname: String by Preference(BaseConstant.FULL_NAME, "")
    var labour_type: String by Preference(BaseConstant.LABOUR_TYPE, "1")
    var id: Int by Preference(BaseConstant.ID, 0)
    var labour_json: String by Preference(BaseConstant.LABOUR_JSON, "1")



    fun LoginSuccess(act: Activity,userName: String?,nickName:String?,score:String?,position:String?,avatar:String?,labour: ArrayList<Labour>?,id:Int) {
        login = true
        setLoUserInfo(userName,nickName,score,position,avatar,labour,id)

        var hasMain = false
        AppManager.getAppManager().activityStack.forEach {
            if (it.localClassName.contains("MainActivity")) {
                hasMain = true
            }
        }
        if (!hasMain) {
            val it = Intent()
            it.setClassName("com.jinduplan.planmanager","com.jinduplan.planmanager.ui.activity.MainActivity")
            act.startActivity(it)
        }
        AppManager.getAppManager().finishAllActExcept(Class.forName("com.jinduplan.planmanager.ui.activity.MainActivity"))
        RxBus.get().send(BusEvent.LoginEvent(true))
    }

    fun logout(act:Activity) {
        login = false
        userName = ""
        nick_name = ""
        score = ""
        position = ""
        avatar = ""
        mProjectId = ""
        mProjectName = ""
        fullname = ""
        labour_type = "1"
        var hasLogin = false
        AppManager.getAppManager().activityStack.forEach {
            if (it.localClassName.contains("LoginAcitivity")) {
                hasLogin = true
            }
        }
        if (!hasLogin) {
            val it = Intent()
            it.setClassName("com.jinduplan.planmanager","com.jinduplan.planmanager.ui.activity.user.LoginAcitivity")
            act.startActivity(it)
        }
        AppManager.getAppManager().finishAllActExcept(Class.forName("com.jinduplan.planmanager.ui.activity.user.LoginAcitivity"),
            Class.forName("com.jinduplan.planmanager.ui.activity.MainActivity"))
    }

    fun goChangpsw(act:Activity) {
        login = false
        userName = ""
        nick_name = ""
        score = ""
        position = ""
        avatar = ""
        mProjectId = ""
        var hasLogin = false
        AppManager.getAppManager().activityStack.forEach {
            if (it.localClassName.contains("LoginAcitivity")) {
                hasLogin = true
            }
        }
        if (!hasLogin) {
            val it = Intent()
            it.setClassName("com.jinduplan.planmanager","com.jinduplan.planmanager.ui.activity.user.ChangePswAcitivity")
            act.startActivity(it)
        }
        AppManager.getAppManager().finishAllActExcept(Class.forName("com.jinduplan.planmanager.ui.activity.user.ChangePswAcitivity"))
    }

    const val BASE_URL = "http://www.jinduplan.com"

    fun setLoUserInfo(
        userName: String?,
        nickName: String?,
        score: String?,
        position: String?,
        avatar: String?,
        labour: ArrayList<Labour>?,
        id: Int
    ) {
        userName?.let {
            this.userName=userName
        }
        nickName?.let {
            this.nick_name=nickName
        }
        score?.let {
            this.score=score
        }
        position?.let {
            this.position=position
        }
        avatar?.let {
            this.avatar = "${BASE_URL}/$avatar"
        }
        labour?.let {
            if (it.size > 0) {
                fullname = it[0].full_name
                labour_type = it[0].type
                this.labour_json=GsonHelper.toJson(labour)
            }
        }
        id?.let {
            this.id=id
        }
    }
}